/**
 * OWASP GoatDroid Project
 * 
 * This file is part of the Open Web Application Security Project (OWASP)
 * GoatDroid project. For details, please see
 * https://www.owasp.org/index.php/Projects/OWASP_GoatDroid_Project
 *
 * Copyright (c) 2011 - The OWASP Foundation
 * 
 * GoatDroid is published by OWASP under the GPLv3 license. You should read and accept the
 * LICENSE before you use, modify, and/or redistribute this software.
 * 
 * @author Jack Mannino, nVisium Security (https://www.nvisiumsecurity.com)
 * @created 2011
 */
package org.owasp.goatdroid.gui.view;

import java.awt.BorderLayout;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JButton;
import org.owasp.goatdroid.gui.Constants;
import org.owasp.goatdroid.gui.config.Config;
import org.owasp.goatdroid.gui.exception.CorruptConfigException;
import java.awt.Point;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

public class ConfigureEditFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTabbedPane jConfigureEditTabbedPane = null;
	private JPanel jEclipseSettingsPanel = null;
	private JLabel jEclipsePathLabel = null;
	private JTextPane jEclipsePathTextPane = null;
	private JButton jEclipsePathButton = null;
	private JButton jEclipseSettingsUpdateButton = null;
	private JPanel jAndroidSettingsPanel = null;
	private JLabel jAndroidSDKPathLabel = null;
	private JLabel jAndroidVirtualDevicesPathLabel = null;
	private JLabel jAndroidProxyHostLabel = null;
	private JLabel jAndroidProxyPortLabel = null;
	private JTextPane jAndroidSDKPathTextPane = null;
	private JTextPane jAndroidVirtualDevicesTextPane = null;
	private JTextPane jAndroidProxyHostTextPane = null;
	private JTextPane jAndroidProxyPortTextPane = null;
	private JButton jAndroidSDKPathButton = null;
	private JButton jAndroidVirtualDevicesButton = null;
	private JButton jAndroidSettingsUpdateButton = null;
	private HashMap<String, String> settings;

	/**
	 * This is the default constructor
	 * 
	 * @throws CorruptConfigException
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public ConfigureEditFrame() throws FileNotFoundException, IOException,
			CorruptConfigException {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 * @throws CorruptConfigException
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	private void initialize() throws FileNotFoundException, IOException,
			CorruptConfigException {
		Config config = new Config();
		settings = config.readConfig();
		this.setSize(600, 400);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Edit Configuration");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJConfigureEditTabbedPane(), BorderLayout.NORTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jConfigureEditTabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getJConfigureEditTabbedPane() {
		if (jConfigureEditTabbedPane == null) {
			jConfigureEditTabbedPane = new JTabbedPane();
			jConfigureEditTabbedPane.setPreferredSize(new Dimension(482, 375));
			jConfigureEditTabbedPane.addTab("Eclipse", null,
					getJEclipseSettingsPanel(), null);
			jConfigureEditTabbedPane.addTab("Android", null,
					getJAndroidSettingsPanel(), null);
		}
		return jConfigureEditTabbedPane;
	}

	/**
	 * This method initializes jEclipseSettingsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJEclipseSettingsPanel() {
		if (jEclipseSettingsPanel == null) {
			jEclipsePathLabel = new JLabel();
			jEclipsePathLabel.setText("Eclipse Path");
			jEclipsePathLabel.setLocation(new Point(63, 91));
			jEclipsePathLabel.setSize(new Dimension(88, 16));
			jEclipseSettingsPanel = new JPanel();
			jEclipseSettingsPanel.setLayout(null);
			jEclipseSettingsPanel.add(jEclipsePathLabel, null);
			jEclipseSettingsPanel.add(getJEclipsePathTextPane(), null);
			jEclipseSettingsPanel.add(getJEclipsePathButton(), null);
			jEclipseSettingsPanel.add(getJEclipseSettingsUpdateButton(), null);
		}
		return jEclipseSettingsPanel;
	}

	/**
	 * This method initializes jEclipsePathTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getJEclipsePathTextPane() {
		if (jEclipsePathTextPane == null) {
			jEclipsePathTextPane = new JTextPane();
			jEclipsePathTextPane.setSize(new Dimension(230, 50));
			jEclipsePathTextPane.setLocation(new Point(182, 75));
			jEclipsePathTextPane.setText(settings.get("eclipse_path"));
			jEclipsePathTextPane.setEditable(false);
		}
		return jEclipsePathTextPane;
	}

	/**
	 * This method initializes jEclipsePathButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJEclipsePathButton() {
		if (jEclipsePathButton == null) {
			jEclipsePathButton = new JButton();
			jEclipsePathButton.setText("Browse");
			jEclipsePathButton.setLocation(new Point(445, 84));
			jEclipsePathButton.setSize(new Dimension(75, 29));
			jEclipsePathButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							String fileName = openFileDialog();
							jEclipsePathTextPane.setText(fileName);
						}
					});
		}
		return jEclipsePathButton;
	}

	/**
	 * This method initializes jEclipseSettingsUpdateButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJEclipseSettingsUpdateButton() {
		if (jEclipseSettingsUpdateButton == null) {
			jEclipseSettingsUpdateButton = new JButton();
			jEclipseSettingsUpdateButton.setText("Update Settings");
			jEclipseSettingsUpdateButton.setSize(new Dimension(138, 29));
			jEclipseSettingsUpdateButton.setLocation(new Point(226, 160));
			jEclipseSettingsUpdateButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							try {
								Config.updateEclipseSettings(jEclipsePathTextPane
										.getText());
								showConfigSuccessDialog();
							} catch (FileNotFoundException e1) {
								showConfigFailureDialog();
							} catch (IOException e1) {
								showConfigFailureDialog();
							} catch (CorruptConfigException e1) {
								showConfigFailureDialog();
							}
						}
					});
		}
		return jEclipseSettingsUpdateButton;
	}

	/**
	 * This method initializes jAndroidSettingsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJAndroidSettingsPanel() {
		if (jAndroidSettingsPanel == null) {
			jAndroidProxyPortLabel = new JLabel();
			jAndroidProxyPortLabel
					.setText("<html>Proxy Port<br>(Default: 8080)</html>");
			jAndroidProxyPortLabel.setLocation(new Point(50, 217));
			jAndroidProxyPortLabel.setSize(new Dimension(122, 36));
			jAndroidProxyHostLabel = new JLabel();
			jAndroidProxyHostLabel
					.setText("<html>Proxy Host<br>(Default: Localhost)</html>");
			jAndroidProxyHostLabel.setLocation(new Point(50, 151));
			jAndroidProxyHostLabel.setSize(new Dimension(140, 35));
			jAndroidVirtualDevicesPathLabel = new JLabel();
			jAndroidVirtualDevicesPathLabel.setText("Virtual Devices Path");
			jAndroidVirtualDevicesPathLabel.setLocation(new Point(50, 90));
			jAndroidVirtualDevicesPathLabel.setSize(new Dimension(138, 16));
			jAndroidSDKPathLabel = new JLabel();
			jAndroidSDKPathLabel.setText("SDK Path");
			jAndroidSDKPathLabel.setLocation(new Point(50, 43));
			jAndroidSDKPathLabel.setSize(new Dimension(97, 16));
			jAndroidSettingsPanel = new JPanel();
			jAndroidSettingsPanel.setLayout(null);
			jAndroidSettingsPanel.add(jAndroidSDKPathLabel, null);
			jAndroidSettingsPanel.add(jAndroidVirtualDevicesPathLabel, null);
			jAndroidSettingsPanel.add(jAndroidProxyHostLabel, null);
			jAndroidSettingsPanel.add(jAndroidProxyPortLabel, null);
			jAndroidSettingsPanel.add(getJAndroidSDKPathTextPane(), null);
			jAndroidSettingsPanel
					.add(getJAndroidVirtualDevicesTextPane(), null);
			jAndroidSettingsPanel.add(getJAndroidProxyHostTextPane(), null);
			jAndroidSettingsPanel.add(getJAndroidProxyPortTextPane(), null);
			jAndroidSettingsPanel.add(getJAndroidSDKPathButton(), null);
			jAndroidSettingsPanel.add(getJAndroidVirtualDevicesButton(), null);
			jAndroidSettingsPanel.add(getJAndroidSettingsUpdateButton(), null);
		}
		return jAndroidSettingsPanel;
	}

	/**
	 * This method initializes jAndroidSDKPathTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getJAndroidSDKPathTextPane() {
		if (jAndroidSDKPathTextPane == null) {
			jAndroidSDKPathTextPane = new JTextPane();
			jAndroidSDKPathTextPane.setSize(new Dimension(250, 34));
			jAndroidSDKPathTextPane.setLocation(new Point(200, 43));
			jAndroidSDKPathTextPane.setText(settings.get("sdk_path"));
			jAndroidSDKPathTextPane.setEditable(false);
		}
		return jAndroidSDKPathTextPane;
	}

	/**
	 * This method initializes jAndroidVirtualDevicesTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getJAndroidVirtualDevicesTextPane() {
		if (jAndroidVirtualDevicesTextPane == null) {
			jAndroidVirtualDevicesTextPane = new JTextPane();
			jAndroidVirtualDevicesTextPane.setLocation(new Point(200, 90));
			jAndroidVirtualDevicesTextPane.setSize(new Dimension(250, 38));
			jAndroidVirtualDevicesTextPane
					.setText(settings.get("devices_path"));
			jAndroidVirtualDevicesTextPane.setEditable(false);
		}
		return jAndroidVirtualDevicesTextPane;
	}

	/**
	 * This method initializes jAndroidProxyHostTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getJAndroidProxyHostTextPane() {
		if (jAndroidProxyHostTextPane == null) {
			jAndroidProxyHostTextPane = new JTextPane();
			jAndroidProxyHostTextPane.setLocation(new Point(200, 159));
			jAndroidProxyHostTextPane.setSize(new Dimension(250, 16));
			jAndroidProxyHostTextPane.setText(settings.get("proxy_host"));
		}
		return jAndroidProxyHostTextPane;
	}

	/**
	 * This method initializes jAndroidProxyPortTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getJAndroidProxyPortTextPane() {
		if (jAndroidProxyPortTextPane == null) {
			jAndroidProxyPortTextPane = new JTextPane();
			jAndroidProxyPortTextPane.setLocation(new Point(200, 225));
			jAndroidProxyPortTextPane.setSize(new Dimension(250, 16));
			jAndroidProxyPortTextPane.setText(settings.get("proxy_port"));
		}
		return jAndroidProxyPortTextPane;
	}

	/**
	 * This method initializes jAndroidSDKPathButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJAndroidSDKPathButton() {
		if (jAndroidSDKPathButton == null) {
			jAndroidSDKPathButton = new JButton();
			jAndroidSDKPathButton.setText("Browse");
			jAndroidSDKPathButton.setLocation(new Point(460, 38));
			jAndroidSDKPathButton.setSize(new Dimension(75, 29));
			jAndroidSDKPathButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							String fileName = openFileDialog();
							jAndroidSDKPathTextPane.setText(fileName);
						}
					});
		}
		return jAndroidSDKPathButton;
	}

	/**
	 * This method initializes jAndroidVirtualDevicesButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJAndroidVirtualDevicesButton() {
		if (jAndroidVirtualDevicesButton == null) {
			jAndroidVirtualDevicesButton = new JButton();
			jAndroidVirtualDevicesButton.setText("Browse");
			jAndroidVirtualDevicesButton.setLocation(new Point(460, 85));
			jAndroidVirtualDevicesButton.setSize(new Dimension(75, 29));
			jAndroidVirtualDevicesButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							String fileName = openFileDialog();
							jAndroidVirtualDevicesTextPane.setText(fileName);
						}
					});
		}
		return jAndroidVirtualDevicesButton;
	}

	/**
	 * This method initializes jAndroidSettingsUpdateButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJAndroidSettingsUpdateButton() {
		if (jAndroidSettingsUpdateButton == null) {
			jAndroidSettingsUpdateButton = new JButton();
			jAndroidSettingsUpdateButton.setText("Update Settings");
			jAndroidSettingsUpdateButton.setSize(new Dimension(138, 29));
			jAndroidSettingsUpdateButton.setLocation(new Point(230, 282));
			jAndroidSettingsUpdateButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							try {
								Config.updateAndroidSettings(
										jAndroidSDKPathTextPane.getText(),
										jAndroidVirtualDevicesTextPane
												.getText(),
										jAndroidProxyHostTextPane.getText(),
										jAndroidProxyPortTextPane.getText());
								showConfigSuccessDialog();
							} catch (FileNotFoundException e1) {
								showConfigFailureDialog();
							} catch (IOException e1) {
								showConfigFailureDialog();
							} catch (CorruptConfigException e1) {
								showConfigFailureDialog();
							}
						}
					});
		}
		return jAndroidSettingsUpdateButton;
	}

	public void showConfigSuccessDialog() {
		JOptionPane.showMessageDialog(this, Constants.SETTINGS_UPDATED_SUCCESS);
	}

	public void showConfigFailureDialog() {
		JOptionPane.showMessageDialog(this, Constants.SETTINGS_UPDATE_FAILED);
	}

	private String openFileDialog() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fileChooser.setFileHidingEnabled(false);
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			return file.toString();
		} else {
			return "";
		}
	}
}
